package com.yan.refreshlistcontainer.slice;

import com.yan.refreshlistcontainer.ResourceTable;
import com.yan.refreshlistcontainer.bean.ListBean;
import com.yan.refreshlistcontainer.component.RefreshListContainer;
import com.yan.refreshlistcontainer.listener.RefreshListener;
import com.yan.refreshlistcontainer.provider.RefreshListProvider;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    private RefreshListContainer mRefreshList;
    private List<ListBean> mData = new ArrayList<>();
    private MyEventHandler myHandler;
    private RefreshListProvider refreshListProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mRefreshList = (RefreshListContainer) findComponentById(ResourceTable.Id_refresh_list);

        initList();
        EventRunner runner = EventRunner.create(true);
        myHandler = new MyEventHandler(runner);

        refreshListProvider = new RefreshListProvider(MainAbilitySlice.this, mData);
        mRefreshList.setItemProvider(refreshListProvider);
        mRefreshList.setRefreshListener(new RefreshListener() {
            @Override
            public void pullRefresh() {
                ListBean mListBean = new ListBean();
                mListBean.setAge(999);
                mListBean.setName("刷新新增Item");
                mData.add(0, mListBean);
                myHandler.sendEvent(1000, 3000);
            }

            @Override
            public void upLoadingMore() {
                ListBean mListBean = new ListBean();
                mListBean.setAge(999);
                mListBean.setName("加载更多新增Item");
                mData.add(mListBean);
                myHandler.sendEvent(1000, 3000);
            }
        });
    }

    private class MyEventHandler extends EventHandler {
        private MyEventHandler(EventRunner runner) {
            super(runner);
        }

        // 重写实现processEvent方法
        @Override
        public void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            int eventId = event.eventId;

            switch (eventId) {
                case 1000:
                    getUITaskDispatcher().syncDispatch(new Runnable() {
                        @Override
                        public void run() {
                            refreshListProvider.notifyDataChanged();
                            mRefreshList.completeLoading();
                        }
                    });

                    break;

                default:
                    break;
            }
        }
    }

    private void initList() {
        for (int i = 0; i < 100; i++) {
            ListBean mListBean = new ListBean();
            mListBean.setAge(i);
            mListBean.setName("测试" + i);
            mData.add(mListBean);
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
